﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace huaguwang.hgmanage
{
    public partial class main : PageBaseAdmin
    {
        List<M_T_MenuExp> MenuList;
        private readonly D_T_AdminUser_Exp _dal = new D_T_AdminUser_Exp();
        protected void Page_Load(object sender, EventArgs e)
        {
            var user = this.Exp_GetUser();
            if(user == null)
            {
                Response.Redirect("login.aspx");
            }


            if(user.IsAdmin)
            {
                MenuList = _dal.GetMenuList();
            }
            else
            {

                MenuList = _dal.GetMenuList(user.id);
            }


            var menuDistinct = new LinqDistinct<M_T_MenuExp>(
                (t1, t2) => t1.PID == t2.PID,
                t1 => t1.PID);

            var MenuPList = MenuList.Distinct(menuDistinct).Select(Sel => new M_T_PageURL() { Name = Sel.PName, id = Sel.PID });
            this.Admin_user.Text = GetRealName();
            this.Repeater1.DataSource = MenuPList.ToList();
            this.Repeater1.DataBind();
        }

        protected string GetRealName()
        {
            return Page.Exp_GetUser().RealName;
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var RP = e.Item.FindControl("Repeater2") as Repeater;
            var id = e.Item.DataItem.Exp_To<M_T_PageURL>().id;

            var list = MenuList.
                            Where(Sel => Sel.PID == id).
                            Select(Sel => new M_T_PageURL()
                            {
                                Name = Sel.MenuName,
                                URL = Sel.MenuURL,
                                id = Sel.id
                            });

            RP.DataSource = list.ToList();
            RP.DataBind();

        }

    }
}
